package com.lakeworks.web.controller.core;

import com.lakeworks.common.annotation.Log;
import com.lakeworks.common.core.controller.BaseController;
import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.common.core.page.TableDataInfo;
import com.lakeworks.common.enums.BusinessType;
import com.lakeworks.domain.SysFrame;
import com.lakeworks.domain.vo.FrameCreate;
import com.lakeworks.domain.vo.ReleaseAlloateFrame;
import com.lakeworks.service.ISysFrameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.lakeworks.common.constant.Constants.ECHOSTR;
import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
import static org.springframework.util.StringUtils.hasText;


/**
 * 格架信息Controller
 *
 * @author lake
 * @date 2023-02-08
 */
@Validated
@RestController
@RequestMapping("/system/frame")
public class SysFrameController extends BaseController {
    private String prefix = "system/frame";

    @Autowired
    private ISysFrameService sysFrameService;

    /**
     * redis格架初始化
     */
    @PostMapping("/redis/init")
    public AjaxResult initRedisFrame(String echostr) {
        if (!hasText(echostr) || !ECHOSTR.equals(md5Hex(echostr)))
            return error("echostr有误");
        sysFrameService.initFrame();
        return success();
    }

    /**
     * 批量新增搁架号（草稿）
     * @param frameCreate
     * @return
     */
    @PostMapping("/batch/create")
    public AjaxResult batchCreate(@Valid @RequestBody FrameCreate frameCreate) {

        sysFrameService.batchCreate(frameCreate);

        return success();
    }

    /**
     * 格架列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestBody SysFrame sysFrame) {
        startPage();
        List<SysFrame> list = sysFrameService.selectSysFrameList(sysFrame);
        return getDataTable(list);
    }

    /**
     * 批量释放搁架
     */
    @PostMapping("/batch/release")
    public AjaxResult batchReleaseFrame(@Valid @RequestBody ReleaseAlloateFrame releaseAlloateFrame) {
        if (!ECHOSTR.equals(md5Hex(releaseAlloateFrame.getEchostr())))
            return error("echostr有误");
        logger.info("批量释放搁架，isAtt：{}，frameNos：{}", releaseAlloateFrame.getIsAtt(),releaseAlloateFrame.getFrameNos());
        sysFrameService.batchRelease(releaseAlloateFrame.getIsAtt(),releaseAlloateFrame.getFrameNos());
        return success();
    }

//    /**
//     * 导出
//     */
//    @Log(title = "格架信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public AjaxResult export(SysFrame sysFrame)
//    {
//        List<SysFrame> list = sysFrameService.selectSysFrameList(sysFrame);
//        ExcelUtil<SysFrame> util = new ExcelUtil<SysFrame>(SysFrame.class);
//        return util.exportExcel(list, "格架信息数据");
//    }

    /**
     * 新增保存
     */
    @Log(title = "格架信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addSave(@RequestBody SysFrame sysFrame) {
        return toAjax(sysFrameService.insertSysFrame(sysFrame));
    }

    /**
     * 修改
     */
    @PutMapping("/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SysFrame sysFrame = sysFrameService.selectSysFrameById(id);
        mmap.put("sysFrame", sysFrame);
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    @PutMapping
    public AjaxResult editSave(SysFrame sysFrame) {
        return toAjax(sysFrameService.updateSysFrame(sysFrame));
    }

    /**
     * 删除
     */
    @DeleteMapping
    public AjaxResult remove(@RequestParam String ids) {
        return toAjax(sysFrameService.deleteSysFrameByIds(ids));
    }
}
